package cn.kgc.layui.servlet;

import cn.kgc.layui.dao.RoleDao;
import cn.kgc.layui.dao.impl.RoleDaoImpl;
import cn.kgc.layui.entity.Manager;
import cn.kgc.layui.entity.Role;
import cn.kgc.layui.service.ManagerService;
import cn.kgc.layui.service.impl.ManagerServiceImpl;
import cn.kgc.layui.util.LayUiResult;
import cn.kgc.layui.vo.RoleVO;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/man/*")
public class ManagerServlet extends BaseServlet<Manager> {
    ManagerService managerService=new ManagerServiceImpl();
    RoleDao roleDao=new RoleDaoImpl();


    /**
     * 查询 操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest req, HttpServletResponse resp, Manager manager) throws ServletException, IOException {

        int page = Integer.parseInt(req.getParameter("page"));
        int size = Integer.parseInt(req.getParameter("limit"));
        int start = (page-1)*size;
        LayUiResult<Manager> result = managerService.findByPage(start, size,manager);
        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(result);
        // 通过响应对象获取输出流
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(strResult);
    }

    /**
     * 返回角色下拉框数据
     * @param req
     * @param resp
     * @param manager
     */
    public void roleData(HttpServletRequest req, HttpServletResponse resp, Manager manager) throws IOException {
        List<RoleVO> list= roleDao.roleData();
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(list));
    }

    /**
     * 新增操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest req, HttpServletResponse resp,Manager manager) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {


        int result=managerService.save(manager);
        LayUiResult layUiResult;
        if(result>0){
            layUiResult=LayUiResult.builder().code(200).msg("保存成功").build();
        }else {
            layUiResult=LayUiResult.builder().code(500).msg("保存失败").build();
        }
        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        resp.getWriter().write(strResult);

    }

    /**
     * 修改
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest req, HttpServletResponse resp,Manager manager) throws ServletException, IOException{

        int result=managerService.update(manager);
        LayUiResult layUiResult;
        if(result>0){
            layUiResult=LayUiResult.builder().code(200).msg("修改成功").build();
        }else {
            layUiResult=LayUiResult.builder().code(500).msg("修改失败").build();
        }

        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        // 通过响应对象获取输出流
        resp.getWriter().write(strResult);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp,Manager manager) throws ServletException, IOException{
        // 获取前端传递的参数
        String id= req.getParameter("id");
        String ids= req.getParameter("ids");
        int result=managerService.delete(id,ids);
        LayUiResult layUiResult;
        if(result>0){
            layUiResult=LayUiResult.builder().code(200).msg("删除成功").build();
        }else {
            layUiResult=LayUiResult.builder().code(500).msg("删除失败").build();
        }
        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        resp.getWriter().write(strResult);
    }
}
